package singsong;

import java.util.ArrayList;
import java.util.List;

public class TrackData {
	private float sampleRate;
	private float sampleDuration;
	
	public class Sample {
		private float frequency;
		
		private float volume;
		
		public Sample(float frequency, float volume) {
			this.frequency = frequency;
			this.volume = volume;
		}
		
		public void setFrequency(float frequency) {
			this.frequency = frequency;
		}

		public float getFrequency() {
			return frequency;
		}
		
		public void setVolume(float volume) {
			this.volume = volume;
		}
		
		public float getVolume() {
			return volume;
		}
	}
	
	private List<Sample> samples = new ArrayList<Sample>();
	
	public TrackData(float sampleRate) {
		this.sampleRate = sampleRate;
		this.sampleDuration = 1.0f / sampleRate;
	}
	
	public List<Sample> getSamples() {
		return samples;
	}
	
	public Sample getSampleAtTime(float t) {
		return samples.get(getSampleIndexAtTime(t));
	}
	
	public int getSampleIndexAtTime(float t) {
		return Math.max(0, Math.min(samples.size() - 1, (int) (t / sampleDuration)));
	}

	public float getSampleRate() {
		return sampleRate;
	}
	
	public float getSampleDuration() {
		return sampleDuration;
	}
	
	public void addSample(float frequency, float volume) {
		samples.add(new Sample(frequency, volume));
	}

	public void addSilence(float length) {
		int numSamples = (int) (length / sampleDuration);
		for (int i = 0; i < numSamples; ++i) {
			addSample(0, 0);
		}
	}
}
